//
//  ZMCollectionView.swift
//  LoveBeen
//
//  Created by pg on 2017/8/7.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMCollectionView: UICollectionView ,UICollectionViewDataSource,UICollectionViewDelegate{
   //数据源
    var titleArray: [String]?{
        didSet{
            //计算collectionview的大小
            calculateSize(itemCount: titleArray?.count ?? 0)
            //更新数据
            reloadData()
        }
    }
    
    var flowLayout : UICollectionViewFlowLayout
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: 80, height: 30)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin

        super.init(frame: CGRect.zero, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }
    
    let itemMargin : CGFloat = 5
    let column : CGFloat = 3
    let itemHight : CGFloat = 30
    
    //MARK:- 计算collectionview的size
    private func calculateSize(itemCount: Int){
        
        let itemWid = (UIScreen.main.bounds.width - 100 - (column+1)*itemMargin) / column
        
        //计算cell的行数
        let rows = (itemCount-1) / Int(column) + 1

        let viewHight = CGFloat(rows) * itemHight + CGFloat(rows-1) * itemMargin
        
        //更新约束
        self.snp.updateConstraints { (make) in
            make.height.equalTo(viewHight).priority(.high)
        }
        //计算item大小
        flowLayout.itemSize = CGSize(width: itemWid, height: itemHight)

    }
    
    private func setupUI(){
        self.backgroundColor = UIColor.white
        
        delegate = self
        dataSource = self
        
        //注册cell
        self.register(ZMCell.self, forCellWithReuseIdentifier: "zmcell")

        let itemWid = (UIScreen.main.bounds.width - 100 - (column+1)*itemMargin) / column
        flowLayout.itemSize = CGSize(width: itemWid, height: 30)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

//collectionview数据源方法
extension ZMCollectionView {
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        print(titleArray?.count ?? 0)
        return self.titleArray?.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "zmcell", for: indexPath) as! ZMCell
        
        cell.title = titleArray![indexPath.item]
        cell.layer.cornerRadius = 5
        cell.layer.masksToBounds = true
        return cell
        
    }
    
}



//自定义collectionview的Cell
class ZMCell : UICollectionViewCell{
    
    
    var title: String?{
        didSet{
            titleLabel.text = title
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //初始化UI
        initUI()
        
    }
    
    //懒加载titleLabel
    fileprivate lazy var titleLabel: UILabel = {
       
        let label = UILabel(text: "", fontSize: 14, textColor: UIColor.darkGray)
        label.sizeToFit()
        return label
    }()
    
    private func initUI(){
    
    
        self.contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}






